<template>
  <div class="app-container">
    <el-card>
      <div slot="header">
        <div class="tools-select">
          <el-row>
            <el-col>
              <el-button
                type="info"
                size="mini"
                icon="el-icon-plus"
                @click="handleAdd"
                v-if="$store.getters.roles.includes('SYS_MAJOR_CATEGORY_ADD')"
              >新增
              </el-button>
              <el-button
                type="info"
                size="mini"
                icon="el-icon-download"
                @click="handleDownload"
                v-if="$store.getters.roles.includes('SYS_MAJOR_CATEGORY_DOWNLOAD')"
              >下载模板
              </el-button>
              <el-button
                type="info"
                size="mini"
                icon="el-icon-upload2"
                @click="handleImport"
                v-if="$store.getters.roles.includes('SYS_MAJOR_CATEGORY_IMPORT')"
              >导入结果
              </el-button>
            </el-col>
          </el-row>
        </div>
      </div>
      <!-- 表格区 -->
      <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column align="center" label="序号" type="index" width="100px"/>
        <el-table-column
          prop="label"
          label="名称"
          width="300"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="code"
          label="编码"
          width="300"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="操作时间"
          width="220"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
        >
          <template v-slot="scope">
            <el-button
              type="text"
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scope.row.id)"
              v-if="$store.getters.roles.includes('SYS_MAJOR_CATEGORY_EDIT')"
            >修改
            </el-button>
            <el-button
              type="text"
              size="mini"
              icon="el-icon-delete"
              @click="handleDel(scope.row.id)"
              v-if="$store.getters.roles.includes('SYS_MAJOR_CATEGORY_DELETE')"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <EditDialog ref="edit" @onCallBack="handleQuery"/>
    <ImportDialog ref="import" @onCallBack="handleQuery"/>
  </div>
</template>

<script>
import { getTree, deleteById, downloadExcelTemplate } from '@/api/sysMajorCategory'
import EditDialog from './components/EditDialog'
import ImportDialog from './components/ImportDialog'

export default {
  components: { EditDialog, ImportDialog },
  data() {
    return {
      tableData: []
    }
  },
  mounted() {
    this.handleQuery()
  },
  methods: {
    handleAdd() {
      this.$refs['edit'].onShow()
    },
    handleEdit(id) {
      this.$refs['edit'].onShow(id)
    },
    handleDel(id) {
      deleteById(id).then(res => {
        if (res.code === 20000) {
          this.handleQuery()
        }
      })
    },
    handleDownload() {
      downloadExcelTemplate()
    },
    handleImport() {
      this.$refs['import'].onShow()
    },
    handleQuery() {
      getTree().then(res => {
        if (res.code === 20000) {
          this.tableData = res.data
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
